package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.concurrent.TimeUnit;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import model.Statistik;
import service.Service;

/**
 * 
 * @author Lars, Christian og Nikolaj
 *
 */

public class MainFrame extends JFrame {

	private JPanel contentPane;

	private JMenuItem mntmLagerOversigt, mntmBehandlingOpretBehandling, mntmBehandlingRedigerBehandling, mntmDragering;
	private MenuActionListener menuActionListener = new MenuActionListener();
	private JPanel panelstatistik;
	private JLabel lblGemmemsnitstid, lblTimer, lblMinutter, lblSekunder;
	private JTextField textTimer;
	private JMenuItem mntmLagerOversigtBuffer;
	private JTextField textMinutter, textSekunder;
	private JButton btnVis;
	private JLabel lblProcentForGamle;
	private JLabel lblBehandlingerIAlt;
	private JLabel lblForGamleBehandlinger;
	private JLabel lblProcent;
	private JTextField txfBehandlingerIAlt;
	private JTextField txfForGamleBehandlinger;
	private JTextField txfProcentForGamle;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				// test data
				
				try {
					MainFrame frame = new MainFrame();
					frame.setVisible(true);
					
//					Service.getInstance().createSomeObjects();
					
					frame.addWindowListener(new WindowAdapter() {
					    public void windowClosing(WindowEvent e) {
					        Service.getInstance().storeLager();
					        Service.getInstance().storeStatistik();
					    }
					});
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainFrame() {
		setTitle("Carletti");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 315, 390);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnLager = new JMenu("Lager");
		menuBar.add(mnLager);
		
		mntmLagerOversigt = new JMenuItem("Oversigt");
		mnLager.add(mntmLagerOversigt);
		
		mntmLagerOversigtBuffer = new JMenuItem("Oversigt over bufferlager");
		mnLager.add(mntmLagerOversigtBuffer);
		
		mntmDragering = new JMenuItem("Oversigt over dragering");
		mnLager.add(mntmDragering);
		
		JMenu mnBehandling = new JMenu("Behandling");
		menuBar.add(mnBehandling);
		
		mntmBehandlingOpretBehandling = new JMenuItem("Opret behandling");
		mnBehandling.add(mntmBehandlingOpretBehandling);
		
		mntmBehandlingRedigerBehandling = new JMenuItem("Rediger behandling");
		mnBehandling.add(mntmBehandlingRedigerBehandling);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		panelstatistik = new JPanel();
		contentPane.add(panelstatistik, BorderLayout.CENTER);
		panelstatistik.setLayout(null);
		
		lblGemmemsnitstid = new JLabel("Gemmemsnitstid for t\u00F8rring");
		lblGemmemsnitstid.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblGemmemsnitstid.setBounds(10, 48, 168, 14);
		panelstatistik.add(lblGemmemsnitstid);
		
		textTimer = new JTextField();
		textTimer.setBounds(160, 73, 95, 20);
		panelstatistik.add(textTimer);
		textTimer.setColumns(20);
		textTimer.setEditable(false);
		
		lblTimer = new JLabel("Timer:");
		lblTimer.setBounds(10, 76, 46, 14);
		panelstatistik.add(lblTimer);
		
		lblMinutter = new JLabel("Minutter:");
		lblMinutter.setBounds(10, 101, 59, 20);
		panelstatistik.add(lblMinutter);
		
		textMinutter = new JTextField();
		textMinutter.setEditable(false);
		textMinutter.setBounds(160, 101, 95, 20);
		panelstatistik.add(textMinutter);
		textMinutter.setColumns(10);
		
		textSekunder = new JTextField();
		textSekunder.setEditable(false);
		textSekunder.setBounds(160, 129, 95, 20);
		panelstatistik.add(textSekunder);
		textSekunder.setColumns(10);
		
		lblSekunder = new JLabel("Sekunder:");
		lblSekunder.setBounds(10, 132, 62, 14);
		panelstatistik.add(lblSekunder);
		
		btnVis = new JButton("Udregn");
		btnVis.setBounds(206, 298, 81, 23);
		panelstatistik.add(btnVis);
		
		JLabel lblStatistik = new JLabel("Statistik");
		lblStatistik.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblStatistik.setBounds(10, 11, 111, 14);
		panelstatistik.add(lblStatistik);
		
		lblProcentForGamle = new JLabel("Procent for gamle behandlinger");
		lblProcentForGamle.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblProcentForGamle.setBounds(10, 174, 214, 14);
		panelstatistik.add(lblProcentForGamle);
		
		lblBehandlingerIAlt = new JLabel("Behandlinger i alt:");
		lblBehandlingerIAlt.setBounds(10, 199, 118, 14);
		panelstatistik.add(lblBehandlingerIAlt);
		
		lblForGamleBehandlinger = new JLabel("For gamle behandlinger:");
		lblForGamleBehandlinger.setBounds(10, 224, 147, 14);
		panelstatistik.add(lblForGamleBehandlinger);
		
		lblProcent = new JLabel("Procent:");
		lblProcent.setBounds(10, 249, 111, 14);
		panelstatistik.add(lblProcent);
		
		txfBehandlingerIAlt = new JTextField();
		txfBehandlingerIAlt.setEditable(false);
		txfBehandlingerIAlt.setBounds(160, 196, 96, 20);
		panelstatistik.add(txfBehandlingerIAlt);
		txfBehandlingerIAlt.setColumns(10);
		
		txfForGamleBehandlinger = new JTextField();
		txfForGamleBehandlinger.setEditable(false);
		txfForGamleBehandlinger.setBounds(160, 221, 96, 20);
		panelstatistik.add(txfForGamleBehandlinger);
		txfForGamleBehandlinger.setColumns(10);
		
		txfProcentForGamle = new JTextField();
		txfProcentForGamle.setEditable(false);
		txfProcentForGamle.setBounds(160, 246, 96, 20);
		panelstatistik.add(txfProcentForGamle);
		txfProcentForGamle.setColumns(10);
		
		btnVis.addActionListener(menuActionListener);
		mntmBehandlingRedigerBehandling.addActionListener(menuActionListener);
		mntmBehandlingOpretBehandling.addActionListener(menuActionListener);
		mntmDragering.addActionListener(menuActionListener);
		mntmLagerOversigt.addActionListener(menuActionListener);
		mntmLagerOversigtBuffer.addActionListener(menuActionListener);
	}

	private class MenuActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource().equals(mntmLagerOversigt)) {
				LagerOversigtFrame lof = new LagerOversigtFrame();
				lof.setVisible(true);
			}
			else if(e.getSource() == mntmLagerOversigtBuffer) {
				LagerBufferOversigtFrame lbof = new LagerBufferOversigtFrame();
				lbof.setVisible(true);
			}
			else if(e.getSource() == mntmDragering) {
				DrageringFrame frame = new DrageringFrame();
				frame.setVisible(true);
			}
			else if(e.getSource() == mntmBehandlingOpretBehandling) {
				OpretBehandlingFrame frame = new OpretBehandlingFrame();
				frame.setVisible(true);
			}
			else if(e.getSource() == mntmBehandlingRedigerBehandling) {
				VaelgBehandlingFrame frame = new VaelgBehandlingFrame();
				frame.setVisible(true);
			}
			else if(e.getSource() == btnVis){
				long timer = TimeUnit.MILLISECONDS.toHours(Service.getInstance().generateStatistikGennemsnitstid());
				long minutter = TimeUnit.MILLISECONDS.toMinutes(Service.getInstance().generateStatistikGennemsnitstid());
				long sekunder = TimeUnit.MILLISECONDS.toSeconds(Service.getInstance().generateStatistikGennemsnitstid());
				
				textTimer.setText(timer+"");
				textMinutter.setText(minutter - (timer*60) + "");
				textSekunder.setText(sekunder - (minutter*60) +"");
				
				txfBehandlingerIAlt.setText(Statistik.getInstance().getAntalBehandlinger()+"");
				txfForGamleBehandlinger.setText(Statistik.getInstance().getAntalForGamleBehandlinger()+"");
				txfProcentForGamle.setText(Statistik.getInstance().getForGamleProcent()+"%");
			}
		}
		
	}
}
